package com.niit.dao.impl;

import com.niit.dao.IProductDao;
import com.niit.domain.Product;
import com.niit.utils.DBHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements IProductDao {
    @Override
    public List<Product> queryProduct(Integer page) {
        List<Product> list = new ArrayList<>();
        //编写sql语句
        String sql = "select * from sh_product order by pubdate desc limit ?";
        ResultSet rs = DBHelper.executeQuery(sql, page);
        Product product = null;
        while (true) {
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                product = new Product(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getDate(4), rs.getInt(5), rs.getString(6), rs.getInt(7));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            list.add(product);
        }
        return list;
    }

    @Override
    public List<Product> queryByIs_good(Integer page) {
        List<Product> list = new ArrayList<>();
        String sql = "select * from sh_product where is_good=0 order by pubdate desc limit ?";
        ResultSet rs = DBHelper.executeQuery(sql, page);
        while (true) {
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            Product product = null;
            try {
                product = new Product(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getDate(4), rs.getInt(5), rs.getString(6), rs.getInt(7));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            list.add(product);
        }
        return list;
    }

    @Override
    public List<Product> queryByIs_good2(Integer page) {
        List<Product> list = new ArrayList<>();
        String sql = "select * from sh_product where is_good=0 limit ?";
        ResultSet rs = DBHelper.executeQuery(sql, page);
        while (true) {
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            Product product = null;
            try {
                product = new Product(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getDate(4), rs.getInt(5), rs.getString(6), rs.getInt(7));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            list.add(product);
        }
        return list;
    }

    @Override
    public List<Product> queryProducts(Integer currentPage, Integer pageSize) {

        List<Product> list = new ArrayList<>();
        String sql = "select * from sh_product limit ?,?";
        ResultSet rs = DBHelper.executeQuery(sql, currentPage, pageSize);
        while (true) {
            try {
                if (!rs.next()) break;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            Product product = null;
            try {
                product = new Product(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getDate(4), rs.getInt(5), rs.getString(6), rs.getInt(7));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            list.add(product);
        }
        return list;
    }

    @Override
    public int queryTotal() {
        String sql = "select count(*) from sh_product";
        ResultSet rs = DBHelper.executeQuery(sql);
        int i = 0;
        try {
            if (rs.next()) {

                i = rs.getInt(1);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public Product queryProductByID(Integer id) {
        Product product = null;
        String sql = "select * from sh_product where id = ?";
        ResultSet rs = DBHelper.executeQuery(sql, id);
        try {
            if(rs.next()){
                product = new Product(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getDate(4),rs.getInt(5),rs.getString(6),rs.getInt(7));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return product;
    }

    @Override
    public int productsCountadd(Integer id) {
        String sql = "update sh_product set click_num = click_num+1 where id = ?";
        int i = DBHelper.executeUpdate(sql,id);
        return i;
    }
}
